<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<%@taglib prefix="s" uri="http://www.springframework.org/tags" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <jsp:include page="../../../../../head.jsp"/>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>
        <c:choose>
            <c:when
                    test="${not empty id}">修改角色</c:when>
            <c:otherwise>添加角色</c:otherwise>
        </c:choose></title>
</head>
<script type="text/javascript">
    var id = '${id}';
    var title = "添加角色";
    if (id) {
        title = "修改角色";
    }
    var dropdownState = $.mrdForm.ajaxdp("dropdown-useState");
    var formSetting = {
        title: title,
        module: "save",
        numCount: 1,
        columns: [{
            name: "id",
            hidden: true
        }, {
            name: "name",
            label: "角色名称",
            validate: 'validate[required]'
        }, {
            name: "company.id",
            label: "所属公司",
            type: "dropdown",
            dropdownType: "ajax",
            url: "dropdown-company",
            minLength: 2,
            success: function (data) {
                var array = new Array();
                $.each(data, function (i, n) {
                    array.push([n[0], n[1]]);
                });
                return array;
            }
        }, {
            name: "state",
            label: "启用状态",
            type: "dropdown",
            data: dropdownState,
            value: "0",
            dropdownValue: "启用",
            validate: 'validate[required]'
        }],
        buttons: {
            custom: []
        },
        form: {
            url: "role-save",
            data: function () {
                if (id) {
                    var dataValue = "";
                    $
                            .ajax({
                                      url: "role-beUpdate?id=" + id,
                                      async: false,
                                      success: function (data) {
                                          if (data != null) {
                                              var dropdownName = $.mrdForm
                                                      .getDropDwonLabelName("company.id");
                                              if (data.company) {
                                                  data[dropdownName] = data.company.name;
                                              }
                                          }
                                          dataValue = data;
                                      }
                                  });
                    return dataValue;
                } else {
                    return null;
                }
            },
            validate: {
                options: {
                    promptPosition: "bottomLeft"
                }
            },
            'ajaxForm': {
                success: function (data) {
                    alert(data.msg);
                    if (data.result) {
                        parent.document.getElementById('west').contentWindow.refresh();
                        jw.close();
                    }
                }
            }
        }
    };
    $(document).ready(function () {
        $("#role").mrdForm(formSetting);
        var dropdownName = $.mrdForm
                .getDropDwonLabelName("company.id");
        $("input[name='" + dropdownName + "']").bind("click", function () {
            $(this).autocomplete('search', '');
        });
    });
</script>
<body>
<div id="role"></div>
</body>
</html>
